package top.linyuxb.lycloud.admin.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.linyuxb.lycloud.admin.constant.StatusEnum;
import top.linyuxb.lycloud.admin.entity.LyCloudUserEntity;
import top.linyuxb.lycloud.admin.entity.LyConfClusterEntity;
import top.linyuxb.lycloud.admin.repository.LyConfClusterRepository;
import top.linyuxb.lycloud.admin.service.ILyConfPermissionService;
import top.linyuxb.lycloud.admin.service.impl.LoginService;
import top.linyuxb.lycloud.admin.utils.EntityUtil;
import top.linyuxb.lycloud.admin.utils.ReturnT;
import top.linyuxb.lycloud.admin.web.annotation.PermissionLimit;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static top.linyuxb.lycloud.admin.web.interceptor.EnvInterceptor.CURRENT_ENV;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/7/29 14:43
 * @Desc: 环境管理
 */
@Controller
@RequestMapping("/cluster")
public class ClusterController {
    @Resource
    private LyConfClusterRepository lyConfClusterRepository;
    @Resource
    private ILyConfPermissionService lyConfPermissionService;

    @RequestMapping
    public String index(HttpServletRequest request, Model model) {
        String env = (String) request.getAttribute(CURRENT_ENV);
        LyCloudUserEntity lyCloudUser = (LyCloudUserEntity) request.getAttribute(LoginService.LOGIN_IDENTITY);
        List<LyConfClusterEntity> list = lyConfClusterRepository.findByEnvAndStatusOrderByIdDesc(
                env, StatusEnum.VALID.getType());
        Set<String> envs = new HashSet<>();
        Boolean status = lyConfPermissionService.getPermissionEnvs(lyCloudUser, envs);
        list.removeIf(server -> !status && !envs.contains(server.getEnv()));
        model.addAttribute("list", list);
        return "cluster/cluster.index";
    }

    @ResponseBody
    @RequestMapping("/save")
    @PermissionLimit(adminuser = true)
    public ReturnT<String> save(LyConfClusterEntity lyConfCluster) {
        if (StringUtils.isBlank(lyConfCluster.getEnv())) {
            return new ReturnT<>(500, "Env不可为空");
        }
        if (StringUtils.isBlank(lyConfCluster.getClusterKey())) {
            return new ReturnT<>(500, "请输入集群key");
        }
        if (StringUtils.isBlank(lyConfCluster.getClusterIps())) {
            return new ReturnT<>(500, "请输入集群Ip，英文逗号分割");
        }

        LyConfClusterEntity existEnv = lyConfClusterRepository.findByEnvAndClusterKeyAndStatus(
                lyConfCluster.getEnv(), lyConfCluster.getClusterKey(), StatusEnum.VALID.getType());
        if (existEnv != null) {
            return new ReturnT<>(500, "集群key已存在，请勿重复添加");
        }

        lyConfCluster.setOrder(10);
        lyConfClusterRepository.save(lyConfCluster);
        return ReturnT.SUCCESS;
    }

    @ResponseBody
    @RequestMapping("/update")
    @PermissionLimit(adminuser = true)
    public ReturnT<String> update(LyConfClusterEntity lyConfCluster) {
        if (StringUtils.isBlank(lyConfCluster.getEnv())) {
            return new ReturnT<>(500, "Env不可为空");
        }
        if (StringUtils.isBlank(lyConfCluster.getClusterKey())) {
            return new ReturnT<>(500, "请输入集群key");
        }
        if (StringUtils.isBlank(lyConfCluster.getClusterIps())) {
            return new ReturnT<>(500, "请输入集群Ip，英文逗号分割");
        }
        LyConfClusterEntity existEnv = lyConfClusterRepository.findByIdAndStatus(
                lyConfCluster.getId(), StatusEnum.VALID.getType());
        BeanUtils.copyProperties(lyConfCluster, existEnv, EntityUtil.getNullPropertyNames(lyConfCluster));
        lyConfClusterRepository.save(existEnv);
        return ReturnT.SUCCESS;
    }

    @ResponseBody
    @RequestMapping("/remove")
    @PermissionLimit(adminuser = true)
    public ReturnT<String> remove(Long id) {
        if (Objects.isNull(id)) {
            return new ReturnT<>(500, "参数ID非法");
        }
        lyConfClusterRepository.deleteById(id);
        return ReturnT.SUCCESS;
    }

}
